package com.hro.hro_app.background;

import java.text.SimpleDateFormat;

import android.annotation.SuppressLint;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.widget.RemoteViews;

import com.hro.hro_app.R;
import com.hro.hro_app.dataController.LessonController;
import com.hro.hro_app.model.Lesson;

@SuppressLint("SimpleDateFormat")
public class WidgetProvider extends AppWidgetProvider {

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

		// Get all ids
		ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
		int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
		for (int widgetId : allWidgetIds) {

			RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
			Lesson nextlesson = LessonController.getNextlesson();
			SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm");
			SimpleDateFormat sdfDate = new SimpleDateFormat("dd-MM");

			String date = sdfDate.format(nextlesson.getStart());
			String end = sdfTime.format(nextlesson.getEnd());
			String from = sdfTime.format(nextlesson.getStart());

			String lessontext = nextlesson.getModule() + '\n' + nextlesson.getLocation() + " " + nextlesson.getTeacher() + '\n' + date + " " + from + "-" + end;
			// Set the text
			remoteViews.setTextViewText(R.id.update, lessontext);
			appWidgetManager.updateAppWidget(widgetId, remoteViews);
		}
	}
}